//
//  GZGuideListController.swift
//  GZMarathon
//
//  Created by apple on 2021/11/12.
//

import UIKit
import MJRefresh
import SVProgressHUD
import AVKit
import Kingfisher
class GZGuideListController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    
    @IBOutlet weak var searchBar: UISearchBar!
        
    var dataSource: [GZGuideList] = []
    
    var curPage: Int = 0
        
    override func viewDidLoad() {
        super.viewDidLoad()
        confCollectionView()
        searchBar.delegate = self
        navigationController?.navigationBar.isHidden = true
    }
//
//    override func viewWillDisappear(_ animated: Bool) {
//        super.viewWillDisappear(animated)
//        navigationController?.navigationBar.isHidden = false
//    }
//
//    override func viewWillAppear(_ animated: Bool) {
//        super.viewWillAppear(animated)
//        navigationController?.navigationBar.isHidden = true
//    }

    
    func confCollectionView() {
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "GZGuideListCell", bundle: nil), forCellWithReuseIdentifier: "guide_list")
        collectionView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.curPage = 0
            self?.geGuideList()
        })
//        tableView.mj_footer = MJRefreshAutoFooter(refreshingBlock: {[weak self] in
//            self?.curPage += 1
//            self?.geGuideList()
//        })
        collectionView.mj_header?.beginRefreshing()
    }
    
    func endRefresh(){
        collectionView.mj_header?.endRefreshing()
        collectionView.mj_footer?.endRefreshing()
    }

    func geGuideList(){
        //0:防疫告警  1:健康告警
        ApiProvider.request(EBAPI.getGuideList(name: searchBar.text, type: "3", page: "1", pageSize: "10"), model: GZGuideData.self) { [weak self] data, isSuccess in
            self?.endRefresh()
            guard let dt = data else{
                return
            }
            
            if self?.curPage == 0{
                self?.dataSource.removeAll()
            }
            self?.dataSource.append(contentsOf: dt.data)
            self?.collectionView.reloadData()
            
        }
        
    }
  
}


extension GZGuideListController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 16
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 16;
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 0.1, height: 0.1)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: 0.1, height: 0.1)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let sWidth = UIScreen.main.bounds.width
        let width = (sWidth-48)/2
        return CGSize(width: width, height: 118);
    }
    
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "guide_list", for: indexPath) as! GZGuideListCell
        let guide = dataSource[indexPath.row]
        cell.titleLabel.text = guide.faguide_name
        let urlString = dataSource[indexPath.row].faguide_videourl
        let url = URL(string: urlString)
        cell.imgView.kf.setImage(with: url)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        playVideo(urlstr: dataSource[indexPath.row].faguide_videourl)
        
    }
    
    
    func playVideo(urlstr: String) {
        guard let url = URL(string: urlstr) else{
            SVProgressHUD.showError(withStatus: "暂无视频链接")
            return
        }
        let playerVC = AVPlayerViewController()
        let player = AVPlayer(playerItem: AVPlayerItem(url: url))
        playerVC.player = player
        player.play()
        self.present(playerVC, animated: true, completion: nil)
    }
    
    
}



extension GZGuideListController: UISearchBarDelegate{
    
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        self.geGuideList()
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        self.geGuideList()
    }
    
    
}
